From f916188e4ed2763f35dbe7c6651dd7a8a8541185 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Mon, 1 Jun 2015 11:40:49 -0700 Subject: [PATCH] Cache the canonical URL for a source This avoids reallocating and recalculating it on each call to SourceIdInner::{eq, hash}, which are called quite often in the backend. --- src/cargo/core/source.rs | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/cargo/core/source.rs b/src/cargo/core/source.rs index 7551161bc..6583f99a7 100644 --- a/src/cargo/core/source.rs +++ b/src/cargo/core/source.rs @@ -72,6 +72,7 @@ pub struct SourceId { #[derive(Eq, Clone, Debug)] struct SourceIdInner { url: Url, + canonical_url: Url, kind: Kind, // e.g. the exact git revision of the specified branch for a Git Source precise: Option @@ -82,6 +83,7 @@ impl SourceId { SourceId { inner: Arc::new(SourceIdInner { kind: kind, + canonical_url: git::canonicalize_url(&url), url: url, precise: None, }), @@ -315,10 +317,9 @@ impl PartialEq for SourceIdInner { if self.kind != other.kind { return false } if self.url == other.url { return true } - match (&self.kind, &other.kind, &self.url, &other.url) { - (&Kind::Git(ref ref1), &Kind::Git(ref ref2), u1, u2) => { - ref1 == ref2 && - git::canonicalize_url(u1) == git::canonicalize_url(u2) + match (&self.kind, &other.kind) { + (&Kind::Git(ref ref1), &Kind::Git(ref ref2)) => { + ref1 == ref2 && self.canonical_url == other.canonical_url } _ => false, } @@ -329,8 +330,8 @@ impl hash::Hash for SourceId { fn hash(&self, into: &mut S) { self.inner.kind.hash(into); match *self.inner { - SourceIdInner { kind: Kind::Git(..), ref url, .. } => { - git::canonicalize_url(url).hash(into) + SourceIdInner { kind: Kind::Git(..), ref canonical_url, .. } => { + canonical_url.hash(into) } _ => self.inner.url.hash(into), } -- 2.30.2